package com.ruoyi.api.zhifubao.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.api.zhifubao.config.AlipayTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlipayRefundService {

    @Autowired
    private AlipayTemplate alipayTemplate;



    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCImoYKGom8cfLrTE5dp7AhGbbk6/cgtkMM1AWNBrZ0HohAT/9t5m9Af0vo4RMJk1d1DWHFJLKRNtpBCTau9KpbhQWcDSs4oxLO1rEdUt+He9V0OT7hvpedstPE2So9el+9jLg2mueYZCZpgsVK9U4w5JqfTh1Ew20ZmGmC5AS4S+dvdEfce58DGdM/3QXAszo4NCuRFPwksSqF6wqclKUPzPIEGeZvQXr3f5AlYUdtSEspk4PFA/EFe8kFlj0S5Q8aWkcKR8BbDN9zPxCovMViqsNXa58umrX3S9tREBb7FzEA7ixmqwTZV5cK/Tpk+J9q8RtqJ6AmePdOaigdN1eBAgMBAAECggEBAIAuP0iLzmL9qawRVhebmjH1JyF4KdSQMip/aRYEDoC3hbKTXmOJryseR+SmDm8VyyMTA4MyBD0u823Db4SMIK7wC2t7D+t9WdCstFWHxigjkxAUB2VcqnhXrU8xV+mIQv5iZRkYEel1UHYBN5A2zsAiQtTpP52eo2k5Ewi3XZZwg9h38xtuST2d2dfmYPqYI1ziYtVWhT4mMEp2Wxbo3BJPLIin7ZZ2+NxHrcY6SJUC+7MrlG1aW3DNN7lBJVRH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Kh+rdaGGkQiXM0GYqibfoRgB9CUuzsLcrvpJZE0WsEEN6VY4j7mGOwS+TV6+cS5T8mlu+c5vod9iXDJ5UpSb0NGBBclBhe9Z2CwlnLNqtJBzi1V9KnlAhwJ3r+fLCggBRwAp5FGp9Vt5a0DuHi15A8HLNvrxVmS+zM4O1lgBGc/I8x6oUXVwkLmBUTastP91CvePm/3A2fnFmpcVuIhJzuixbMQO686AXGFv1bRUjZxcCpzYXkspOTv3WzAp7FyJOfUdTKQCJ6EW11giwlCkZC7Clq6yvQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000132691049");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
        model.setOutTradeNo("17867986486");
        model.setOutRequestNo("HZ01RF001");
        request.setBizModel(model);
        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

//    /**
//     * 支付宝退款接口
//     *
//     * @param outTradeNo   商户订单号
//     * @param outRequestNo 商户退款请求号（唯一标识本次退款）
//     * @param refundAmount 退款金额（单位：元）
//     * @param refundReason 退款原因
//     * @return 退款结果
//     */
//    public boolean refund(String outTradeNo, String refundAmount,
//                          String outRequestNo, String refundReason) {
//        try {
//            // 打印关键参数（调试用）
//            System.out.println("退款参数 - 订单号: " + outTradeNo);
//            System.out.println("退款金额: " + refundAmount);
//            System.out.println("退款请求号: " + outRequestNo);
//            System.out.println("退款原因: " + refundReason);
//
//            // 调用支付宝退款API（根据SDK版本选择合适的重载方法）
//            AlipayTradeRefundResponse response;
//            if (outRequestNo != null && refundReason != null) {
//                // 带商户退款号和退款原因的调用（推荐）
//                response = Factory.Payment.Common().refund(
//                        outTradeNo,
//                        refundAmount
//                );
//            } else {
//                // 基本调用
//                response = Factory.Payment.Common().refund(
//                        outTradeNo,
//                        refundAmount
//                );
//            }
//
//            // 打印原始响应（关键调试步骤）
//            System.out.println("支付宝原始响应: " + response);
//
//            // 检查响应状态
//            if (ResponseChecker.success(response)) {
//                System.out.println("退款请求成功，支付宝响应码: " + response.getCode());
//                return true;
//            } else {
//                System.err.println("退款业务失败 - 错误码: " + response.getCode());
//                System.err.println("错误信息: " + response.getMsg());
//                System.err.println("子错误信息: " + response.getSubMsg());
//                return false;
//            }
//
//        } catch (Exception e) {
//            System.err.println("退款异常: " + e.getClass().getName() + " - " + e.getMessage());
//            e.printStackTrace();
//            return false;
//        }
//    }
//
//
//    /**
//     * 查询退款结果
//     *
//     * @param outTradeNo   商户订单号
//     * @param outRequestNo 商户退款请求号
//     * @return 退款查询结果
//     */
//    public String queryRefundResult(String outTradeNo, String outRequestNo) {
//        try {
//            AlipayTradeFastpayRefundQueryResponse response =
//                    Factory.Payment.Common().queryRefund(outTradeNo, outRequestNo);
//
//            // 根据实际响应对象属性获取状态（以refund_status为例）
//            return response.getRefundStatus();
//
//            // 或通过Map获取（通用方式）
//            // return response.getBodyMap().get("refund_status");
//        } catch (Exception e) {
//
//            return "ERROR";
//        }
//    }
}
